const jwt = require('koa-jwt')
const Router = require('koa-router')
const router = new Router({ prefix: '/users' })
const {
    find, findById, create, update,
    delete: del, login, checkOwner,
    listFollowing, follow, unfollow,
    listFollowers, checkUserExist,
    followTopic, unfollowTopic, listFollowingTopics,
    listQuestions
} = require('../controllers/users')
const { checkTopicExist } = require('../controllers/topics')
const { secret } = require("../config")

// 一个中间件就是一个函数
// 认证，解析token，并且从token中获取用户信息
const auth = jwt({ secret })
// 查找列表
router.get('/', find)
// 创建
router.post('/', create)
// 通过id查询用户
router.get('/:id', findById)
// 修改
router.patch('/:id', auth, checkOwner, update)
// 删除
router.delete('/:id', auth, checkOwner, del)
// 登录
router.post('/login', login)

// 关注某人
router.put(`/following/:id`, auth, checkUserExist, follow)
// 取消关注某人
router.delete(`/following/:id`, auth, checkUserExist, unfollow)
// 获取某个用户的关注的人
router.get(`/:id/following`, listFollowing)
// 获取某个用户的粉丝
router.get(`/:id/followers`, listFollowers)

// 关注话题
router.put(`/followingTopics/:id`, auth, checkTopicExist, followTopic)
// 取消关注话题
router.delete(`/followingTopics/:id`, auth, checkTopicExist, unfollowTopic)
// 获取某个用户的关注的话题
router.get(`/:id/followingTopics`, listFollowingTopics)
// 获取某个用户提出的问题
router.get(`/:id/questions`, listQuestions)
module.exports = router